<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>找两个和为目标值且不重叠的子数组</title>
</head>

<body>
  <script>
    var minSumOfLengths = function (arr, target) {
      const n = arr.length;
      const dp = new Array(n + 1).fill(Infinity);
      let res = Infinity;
      for (let i = 0, j = 0, win = 0; i < n; i++) {
        win += arr[i];
        while (win > target) win -= arr[j++];
        if (win === target) {
          const len = i - j + 1;
          res = Math.min(res, dp[j] + len);
          dp[i + 1] = Math.min(len, dp[i]);
        } else {
          dp[i + 1] = dp[i];
        }
      }
      return res === Infinity ? -1 : res;
    };
    console.log(minSumOfLengths([3, 2, 2, 4, 3], 3));
  </script>
</body>

</html>